package com.cardone.platform.usercenter.controller;

import java.util.List;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.usercenter.dto.OrgDto;
import com.cardone.platform.usercenter.service.OrgService;

/**
 * 组织
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/usercenter/org/")
@Controller
public class OrgController {
	/**
	 * 初始化数据绑定
	 *
	 * @param webDataBinder
	 *          数据绑定
	 */
	// @InitBinder
	// @Deprecated
	// public void initBinder(final WebDataBinder webDataBinder) {
	// }

	/**
	 * 组织：删除
	 *
	 * @param deleteOrg
	 *          组织对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Callable<ReturnDataDto<String>> deleteJson(final OrgDto deleteOrg) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(OrgService.class).deleteByIds(deleteOrg);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, OrgController.log);
				}
			}
		};
	}

	/**
	 * 组织：分页查询
	 *
	 * @param findOrg
	 *          组织对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
	public void editHtml(final OrgDto findOrg, final Model model) {
		final OrgDto org = ContextHolder.getBean(OrgService.class).findById(OrgDto.class, findOrg);

		model.addAttribute("org", org);
	}

	/**
	 * 组织：编辑
	 *
	 * @param findOrg
	 *          组织对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<OrgDto>> findByIdJson(final OrgDto findOrg, final Model model) {
		return new Callable<ReturnDataDto<OrgDto>>() {
			@Override
			public ReturnDataDto<OrgDto> call() throws Exception {
				try {
					final OrgDto org = ContextHolder.getBean(OrgService.class).findById(OrgDto.class, findOrg);

					return ReturnDataUtils.newReturnData(org);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, OrgController.log);
				}
			}
		};
	}

	/**
	 * 组织：查询
	 *
	 * @param findListOrg
	 *          组织对象
	 * @return 组织对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<List<OrgDto>>> findListByLikeCodeJson(final OrgDto findListOrg) {
		return new Callable<ReturnDataDto<List<OrgDto>>>() {
			@Override
			public ReturnDataDto<List<OrgDto>> call() throws Exception {
				try {
					final List<OrgDto> orgList = ContextHolder.getBean(OrgService.class).findListByLikeCode(OrgDto.class, findListOrg);

					return ReturnDataUtils.newReturnData(orgList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, OrgController.log);
				}
			}
		};
	}

	/**
	 * 组织：分页查询
	 *
	 * @param paginationOrg
	 *          组织对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<PaginationDto<OrgDto>>> paginationByLikeCodeJson(final OrgDto paginationOrg) {
		return new Callable<ReturnDataDto<PaginationDto<OrgDto>>>() {
			@Override
			public ReturnDataDto<PaginationDto<OrgDto>> call() throws Exception {
				try {
					final PaginationDto<OrgDto> orgPagination = ContextHolder.getBean(OrgService.class).paginationByLikeCode(OrgDto.class, paginationOrg);

					return ReturnDataUtils.newReturnData(orgPagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, OrgController.log);
				}
			}
		};
	}

	/**
	 * 组织：分页查询
	 *
	 * @param paginationOrg
	 *          组织对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
	public void paginationHtml(final OrgDto paginationOrg, final Model model) {
		final PaginationDto<OrgDto> orgPagination = ContextHolder.getBean(OrgService.class).paginationByLikeCode(OrgDto.class, paginationOrg);

		model.addAttribute("orgPagination", orgPagination);
	}

	/**
	 * 组织：保存
	 *
	 * @param saveOrg
	 *          组织对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
	public Callable<ReturnDataDto<String>> saveJson(final OrgDto saveOrg) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(OrgService.class).saveByIdOrCode(OrgDto.class, saveOrg);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, OrgController.log);
				}
			}
		};
	}
}
